"""
@File    ：run_new.py
@Author  ：ixiaodu
@Date    ：2024/8/16
@Desc    ：
"""
import subprocess
from time import sleep
import pytest

from allure_des.allure_des import set_report_categories_on_results, set_report_env_on_results
from config.config import cm


def run_pytest(test_dir, mark, output_dir):
    try:
        subprocess.run(
            ['pytest', '-vs', test_dir, '-m', mark, f'--alluredir={output_dir}'],
            check=True
        )
    except subprocess.CalledProcessError as e:
        print(f"Pytest execution failed: {e}")
        raise


def generate_allure_report(input_dir, output_dir):
    try:
        subprocess.run(
            ['allure', 'generate', input_dir, '-o', output_dir, '--clean'],
            check=True, shell=True
        )
    except subprocess.CalledProcessError as e:
        print(f"Allure report generation failed: {e}")
        raise


def serve_allure_report(report_dir, port):
    try:
        subprocess.run(
            ['allure', 'serve', report_dir, '-p', str(port)],
            check=True, shell=True
        )
    except subprocess.CalledProcessError as e:
        print(f"Allure report serving failed: {e}")
        raise


def main():
    pytest.main(['-vs', './test_cases/repair_management', '-m', 'repair', '--alluredir=%s' % cm.dir_report_json])
    # 在json目录下创建categories.json文件
    set_report_categories_on_results()
    # 在json目录下创建environment.properties文件
    set_report_env_on_results()
    # 在json目录下创建executor.json文件
    # set_report_executer_on_results()
    sleep(3)

    # 使用封装好的函数
    generate_allure_report(cm.dir_report_json, cm.dir_report_html)
    serve_allure_report("./reports/json", 9999)


if __name__ == '__main__':
    main()
